package com.example.chuntiao.cophook.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.signature.ObjectKey;
import com.example.chuntiao.cophook.R;
import com.example.chuntiao.cophook.beans.Org;
import com.example.chuntiao.cophook.mytools.MyApp;

import java.util.ArrayList;

public class OrgAdapter extends RecyclerView.Adapter<OrgAdapter.ViewHolder> {
    private static final String TAG = "OrgAdapter";


    private ArrayList<Org> mOrgList;
    private Context mContext;
    private OnOrgClickListener mListener;
    private boolean showMore = false;

    public OrgAdapter(ArrayList<Org> fruitList) {
        mOrgList = fruitList;
    }

    public OrgAdapter() {
        mOrgList = new ArrayList<>();
    }

    public OrgAdapter(boolean showMore) {
        mOrgList = new ArrayList<>();
        this.showMore = showMore;
    }

    public void setMenuVisibility(boolean showMenu) {
        this.showMore = showMenu;
    }

    public interface OnOrgClickListener {
        void onClickPic(View v, int position);
        void onClickMore(View v,int position);
    }

    public void setOnClickListener(OnOrgClickListener mListener) {
        this.mListener = mListener;
    }


    static class ViewHolder extends RecyclerView.ViewHolder {
        View orgView;
        ImageView orgIcon;
        TextView orgName;
        ImageView imgMore;

        public ViewHolder(View view) {
            super(view);
            orgView = view;
            imgMore = (ImageView) view.findViewById(R.id.more_option);
            orgIcon = (ImageView) view.findViewById(R.id.image_group);
            orgName = (TextView) view.findViewById(R.id.text_group);
        }
    }

    @Override
    public ViewHolder onCreateViewHolder(final ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.org_item, parent, false);
        if (mContext == null) {
            mContext = parent.getContext();
        }
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        Org org = mOrgList.get(position);
        //holder.orgIcon.setImageResource(org.getId());
        holder.orgName.setText(org.getOname());
        holder.orgIcon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mListener !=null)
                    mListener.onClickPic(v, position);
            }
        });

        if (showMore) {
            holder.imgMore.setVisibility(View.GONE);
        }

        holder.imgMore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mListener!=null)
                    mListener.onClickMore(v,position);
            }
        });
        if (org.getImgVer() != -1) {
            Glide.with(mContext).load(MyApp.HOST_URL + "image/o_" + org.getOid())
                    .transition(new DrawableTransitionOptions().crossFade())
                    .apply(RequestOptions.signatureOf(new ObjectKey(org.getImgVer()))).into(holder.orgIcon);
        }

    }

    @Override
    public int getItemCount() {
        return mOrgList.size();
    }

    public void setmOrgList(ArrayList<Org> mOrgList) {
        this.mOrgList = mOrgList;
    }

    public ArrayList<Org> getmOrgList() {
        return mOrgList;
    }


}